package com.nbjtjc.safe.service.edu;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.edu.EduDailyParticipantMapper;
import com.nbjtjc.safe.model.edu.EduDailyParticipant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EduDailyParticipantService {
	@Autowired
	  private EduDailyParticipantMapper eduDailyParticipantMapper;
		
	  public Result add(EduDailyParticipant eduDailyParticipant){
		        Result result=new Result();
		        if(ObjectUtil.isNull(eduDailyParticipant)
		        		||ObjectUtil.isNull(eduDailyParticipant.getEduDailyInfoId())
//		        		||ObjectUtil.isNull(eduDailyParticipant.getReqInvestId())
		        		||ObjectUtil.isNull(eduDailyParticipant.getUserId())
		        		){		            
		        	result.setFalse("关键字段用户ID和日常培训信息ID不能为空");
		            return result;
		        }
//主键为空或库中无此主键则为新建，插入数据
		        if(ObjectUtil.isNull(eduDailyParticipant.getId())|| ObjectUtil.isNull(eduDailyParticipantMapper.selectByPrimaryKey(eduDailyParticipant.getId()))){
		        	eduDailyParticipant.setCancel(0);
		        	int res=eduDailyParticipantMapper.insertSelective(eduDailyParticipant);
			        if(res==0){
			            result.setFalse("插入参与人员失败");
			            return result;
			        }
		        }else
		        {
		        	int res=eduDailyParticipantMapper.updateByPrimaryKeySelective(eduDailyParticipant);
		        	 if(res==0){
		        		 result.setFalse("更新参与人员失败");
				         return result;

		        	 }
		        }
		        result.setData(eduDailyParticipant);

		        return result;
		    }


	  
	  public Result delete(int eduDailyParticipantId){
	      Result result=new Result();
	      if(ObjectUtil.isNull(eduDailyParticipantId)){
	          result.setFalse("删除对象不能为空");
	          return result;
	      }
	      int res = eduDailyParticipantMapper.deleteByPrimaryKey(eduDailyParticipantId);
	      if(res==0){
	    	  result.setFalse("删除失败");
		      return result;
	
	      }
	      return result;
	  }
	  
	  

	  
	  public Result find(EduDailyParticipant eduDailyParticipant) {
	      Result result = new Result();
	      PageHelper.startPage(eduDailyParticipant.getPageCurrent(), eduDailyParticipant.getPageSize());
	      List<EduDailyParticipant> eduDailyParticipants = eduDailyParticipantMapper.searchDetail(eduDailyParticipant);
	      result.setData(new PageInfo<>(eduDailyParticipants));

	      return result;
	  }
}
